import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store/index";
import routes from "./routes";
Vue.use(VueRouter);

const router = new VueRouter({
  mode: "hash",
  routes,
});

router.beforeEach((to, from, next) => {
  if (store.state.isLogin) {
    if (to.path === "/login") {
      next(from.path);
    }
  }

  if (to.meta.check) {
    if (store.state.isLogin) {
      next();
    } else {
      Vue.prototype.$message.error("需要登录");
      next("/login");
    }
  } else {
    next();
  }
});
router.afterEach((to) => {
  if (to?.meta?.title) {
    document.title = to.meta.title;
  } else {
    document.title = "NetEasyMusic";
  }
});

export default router;
